package org.laizili.solution.leetcode;

/**
 * <a href="https://leetcode.cn/problems/subarray-product-less-than-k/">713. 乘积小于 K 的子数组</a>
 * <p>
 * tags: 双指针（滑动窗口）;
 * <p>
 * notes: 计数
 */
public class Problem713 {
    private static class Solution {
        public int numSubarrayProductLessThanK(int[] nums, int k) {
            if (k <= 1)
                return 0;
            int cnt = 0;
            for (int left = 0, right = 0, product = 1; right < nums.length; right++) {
                product *= nums[right];
                while (product >= k) {
                    product /= nums[left++];
                }
                /*移动右指针，新增多少个满足条件的子数组*/
                cnt += right - left + 1;
            }
            return cnt;
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution().numSubarrayProductLessThanK(new int[]{7, 9, 11}, 5));
    }
}
